<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=11"/>
<meta name="generator" content="Doxygen 1.9.4"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>miniFOC: algorithm/encoder.c Source File</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr id="projectrow">
  <td id="projectlogo"><img alt="Logo" src="Acme_LOGO.png"/></td>
  <td id="projectalign">
   <div id="projectname">miniFOC<span id="projectnumber">&#160;1.0.3</span>
   </div>
   <div id="projectbrief">This open-source project aims to accomplish a FOC(Field Oriented Control) scheme that is operatable with minimum costs in China.</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.9.4 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
var searchBox = new SearchBox("searchBox", "search",'Search','.html');
/* @license-end */
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(function() {
  initMenu('',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
/* @license-end */
</script>
<div id="main-nav"></div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div id="nav-path" class="navpath">
  <ul>
<li class="navelem"><a class="el" href="dir_99d3723365464dd656f512988bb8dbbd.html">algorithm</a></li>  </ul>
</div>
</div><!-- top -->
<div class="header">
  <div class="headertitle"><div class="title">encoder.c</div></div>
</div><!--header-->
<div class="contents">
<a href="encoder_8c.html">Go to the documentation of this file.</a><div class="fragment"><div class="line"><a id="l00001" name="l00001"></a><span class="lineno">    1</span><span class="comment">/**************************************************************************/</span></div>
<div class="line"><a id="l00010" name="l00010"></a><span class="lineno">   10</span><span class="preprocessor">#include &quot;<a class="code" href="encoder_8h.html">encoder.h</a>&quot;</span></div>
<div class="line"><a id="l00011" name="l00011"></a><span class="lineno">   11</span><span class="preprocessor">#include &quot;<a class="code" href="spi_8h.html">spi.h</a>&quot;</span></div>
<div class="line"><a id="l00012" name="l00012"></a><span class="lineno">   12</span><span class="preprocessor">#include &quot;gd32f1x0.h&quot;</span></div>
<div class="line"><a id="l00013" name="l00013"></a><span class="lineno">   13</span><span class="preprocessor">#include &quot;<a class="code" href="foc_8h.html">foc.h</a>&quot;</span></div>
<div class="line"><a id="l00014" name="l00014"></a><span class="lineno">   14</span><span class="preprocessor">#include &quot;<a class="code" href="timer_8h.html">timer.h</a>&quot;</span></div>
<div class="line"><a id="l00015" name="l00015"></a><span class="lineno">   15</span><span class="preprocessor">#include &quot;<a class="code" href="system_8h.html">system.h</a>&quot;</span></div>
<div class="line"><a id="l00016" name="l00016"></a><span class="lineno">   16</span><span class="preprocessor">#include &quot;<a class="code" href="config_8h.html">config.h</a>&quot;</span></div>
<div class="line"><a id="l00017" name="l00017"></a><span class="lineno">   17</span><span class="preprocessor">#include &quot;<a class="code" href="filter_8h.html">filter.h</a>&quot;</span></div>
<div class="line"><a id="l00018" name="l00018"></a><span class="lineno">   18</span> </div>
<div class="line"><a id="l00023" name="l00023"></a><span class="lineno"><a class="line" href="encoder_8c.html#a562eacc7259f25574b1ab8ce9d7c6a3e">   23</a></span><span class="keyword">volatile</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">short</span> <a class="code hl_variable" href="encoder_8c.html#a562eacc7259f25574b1ab8ce9d7c6a3e">machine_angle_offset</a> = 0;</div>
<div class="line"><a id="l00029" name="l00029"></a><span class="lineno"><a class="line" href="encoder_8c.html#af58d29dfca2d46874e2da41b45b48482">   29</a></span><span class="keyword">volatile</span> <span class="keyword">static</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">short</span> <a class="code hl_variable" href="encoder_8c.html#af58d29dfca2d46874e2da41b45b48482">last_mechanical_angle</a> = 0;</div>
<div class="line"><a id="l00034" name="l00034"></a><span class="lineno"><a class="line" href="encoder_8c.html#a97999bca302a3ae095acd9977fee272a">   34</a></span><span class="keyword">volatile</span> <span class="keyword">static</span> <span class="keywordtype">long</span> <span class="keywordtype">long</span> <a class="code hl_variable" href="encoder_8c.html#a97999bca302a3ae095acd9977fee272a">total_machine_angle</a> = 0;</div>
<div class="line"><a id="l00039" name="l00039"></a><span class="lineno"><a class="line" href="encoder_8c.html#aeb44dbae80bf7edaf6189fb8e2278f99">   39</a></span><span class="keyword">volatile</span> <span class="keyword">static</span> <span class="keywordtype">long</span> <span class="keywordtype">long</span> <a class="code hl_variable" href="encoder_8c.html#aeb44dbae80bf7edaf6189fb8e2278f99">systick_mechanical_angle_last</a> = 0;</div>
<div class="line"><a id="l00040" name="l00040"></a><span class="lineno">   40</span> </div>
<div class="line"><a id="l00044" name="l00044"></a><span class="lineno"><a class="line" href="encoder_8c.html#a496fbd2820274bbbc7352c91415e2d3e">   44</a></span><span class="keywordtype">void</span> <a class="code hl_function" href="encoder_8c.html#a496fbd2820274bbbc7352c91415e2d3e">encoder_delay</a>(<span class="keywordtype">void</span>) {</div>
<div class="line"><a id="l00045" name="l00045"></a><span class="lineno">   45</span>    <span class="comment">/* use loop functions to delay time */</span></div>
<div class="line"><a id="l00046" name="l00046"></a><span class="lineno">   46</span>    <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> delay_counter = 0xff;</div>
<div class="line"><a id="l00047" name="l00047"></a><span class="lineno">   47</span>    <span class="keywordflow">while</span> (delay_counter)</div>
<div class="line"><a id="l00048" name="l00048"></a><span class="lineno">   48</span>        delay_counter--;</div>
<div class="line"><a id="l00049" name="l00049"></a><span class="lineno">   49</span>}</div>
<div class="line"><a id="l00050" name="l00050"></a><span class="lineno">   50</span> </div>
<div class="line"><a id="l00056" name="l00056"></a><span class="lineno"><a class="line" href="encoder_8c.html#aa5d6a09bb36a38d53242d3b2401a0d1a">   56</a></span><span class="keywordtype">unsigned</span> <span class="keywordtype">short</span> <a class="code hl_function" href="encoder_8c.html#aa5d6a09bb36a38d53242d3b2401a0d1a">encoder_read_data</a>(<span class="keywordtype">unsigned</span> <span class="keywordtype">short</span> TxData) {</div>
<div class="line"><a id="l00057" name="l00057"></a><span class="lineno">   57</span>    <span class="keywordtype">unsigned</span> <span class="keywordtype">short</span> data;</div>
<div class="line"><a id="l00058" name="l00058"></a><span class="lineno">   58</span> </div>
<div class="line"><a id="l00059" name="l00059"></a><span class="lineno">   59</span>    <span class="comment">/* pull down the CS pin and prepare to send data */</span></div>
<div class="line"><a id="l00060" name="l00060"></a><span class="lineno">   60</span>    gpio_bit_write(GPIOA, GPIO_PIN_4, RESET);</div>
<div class="line"><a id="l00061" name="l00061"></a><span class="lineno">   61</span>    <a class="code hl_function" href="encoder_8c.html#a496fbd2820274bbbc7352c91415e2d3e">encoder_delay</a>();</div>
<div class="line"><a id="l00062" name="l00062"></a><span class="lineno">   62</span> </div>
<div class="line"><a id="l00063" name="l00063"></a><span class="lineno">   63</span>    <span class="comment">/* call SPI related functions to send and receive data */</span></div>
<div class="line"><a id="l00064" name="l00064"></a><span class="lineno">   64</span>    data = <a class="code hl_function" href="spi_8c.html#abbbc27ff0ee7fe2241ea2935b92d66cc">spi_readwrite_halfworld</a>(TxData);</div>
<div class="line"><a id="l00065" name="l00065"></a><span class="lineno">   65</span>    <a class="code hl_function" href="encoder_8c.html#a496fbd2820274bbbc7352c91415e2d3e">encoder_delay</a>();</div>
<div class="line"><a id="l00066" name="l00066"></a><span class="lineno">   66</span> </div>
<div class="line"><a id="l00067" name="l00067"></a><span class="lineno">   67</span>    <span class="comment">/* pull up CS pin to end sending data */</span></div>
<div class="line"><a id="l00068" name="l00068"></a><span class="lineno">   68</span>    gpio_bit_write(GPIOA, GPIO_PIN_4, SET);</div>
<div class="line"><a id="l00069" name="l00069"></a><span class="lineno">   69</span>    <span class="keywordflow">return</span> data;</div>
<div class="line"><a id="l00070" name="l00070"></a><span class="lineno">   70</span>}</div>
<div class="line"><a id="l00071" name="l00071"></a><span class="lineno">   71</span> </div>
<div class="line"><a id="l00076" name="l00076"></a><span class="lineno"><a class="line" href="encoder_8c.html#a28beb7b571ff9e6d39118c8d1324e154">   76</a></span><span class="keywordtype">unsigned</span> <span class="keywordtype">short</span> <a class="code hl_function" href="encoder_8c.html#a28beb7b571ff9e6d39118c8d1324e154">encoder_get_mechanical_angle</a>(<span class="keywordtype">void</span>) {</div>
<div class="line"><a id="l00077" name="l00077"></a><span class="lineno">   77</span>    <span class="comment">/* read back register raw data */</span></div>
<div class="line"><a id="l00078" name="l00078"></a><span class="lineno">   78</span>    <span class="keywordtype">unsigned</span> <span class="keywordtype">short</span> angle = <a class="code hl_function" href="encoder_8c.html#aa5d6a09bb36a38d53242d3b2401a0d1a">encoder_read_data</a>(0x0000) &gt;&gt; 4;</div>
<div class="line"><a id="l00079" name="l00079"></a><span class="lineno">   79</span> </div>
<div class="line"><a id="l00080" name="l00080"></a><span class="lineno">   80</span>    <span class="comment">/* statistical total rotation angle */</span></div>
<div class="line"><a id="l00081" name="l00081"></a><span class="lineno">   81</span>    <a class="code hl_variable" href="encoder_8c.html#a97999bca302a3ae095acd9977fee272a">total_machine_angle</a> = <a class="code hl_variable" href="encoder_8c.html#a97999bca302a3ae095acd9977fee272a">total_machine_angle</a> + angle - <a class="code hl_variable" href="encoder_8c.html#af58d29dfca2d46874e2da41b45b48482">last_mechanical_angle</a>;</div>
<div class="line"><a id="l00082" name="l00082"></a><span class="lineno">   82</span>    <a class="code hl_variable" href="encoder_8c.html#af58d29dfca2d46874e2da41b45b48482">last_mechanical_angle</a> = angle;</div>
<div class="line"><a id="l00083" name="l00083"></a><span class="lineno">   83</span> </div>
<div class="line"><a id="l00084" name="l00084"></a><span class="lineno">   84</span>    <span class="comment">/* returns the result of subtracting the offset. */</span></div>
<div class="line"><a id="l00085" name="l00085"></a><span class="lineno">   85</span>    <span class="keywordflow">return</span> ((<a class="code hl_variable" href="encoder_8c.html#a97999bca302a3ae095acd9977fee272a">total_machine_angle</a> - <a class="code hl_variable" href="encoder_8c.html#a562eacc7259f25574b1ab8ce9d7c6a3e">machine_angle_offset</a>) % <a class="code hl_define" href="config_8h.html#a922433e3d0d117291cc2725164f21718">ENCODER_RESO</a>);</div>
<div class="line"><a id="l00086" name="l00086"></a><span class="lineno">   86</span>}</div>
<div class="line"><a id="l00087" name="l00087"></a><span class="lineno">   87</span> </div>
<div class="line"><a id="l00093" name="l00093"></a><span class="lineno"><a class="line" href="encoder_8c.html#a8fbcb19c2f15c20c1a862e0cf64f6771">   93</a></span><span class="keywordtype">float</span> <a class="code hl_function" href="encoder_8c.html#a8fbcb19c2f15c20c1a862e0cf64f6771">encoder_get_electronic_angle</a>(<span class="keywordtype">void</span>) {</div>
<div class="line"><a id="l00094" name="l00094"></a><span class="lineno">   94</span>    <span class="comment">/* read back the mechanical angle directly from the magnetic encoder */</span></div>
<div class="line"><a id="l00095" name="l00095"></a><span class="lineno">   95</span>    <span class="keywordtype">unsigned</span> <span class="keywordtype">short</span> tmp_mechanical_angle = <a class="code hl_function" href="encoder_8c.html#a28beb7b571ff9e6d39118c8d1324e154">encoder_get_mechanical_angle</a>();</div>
<div class="line"><a id="l00096" name="l00096"></a><span class="lineno">   96</span> </div>
<div class="line"><a id="l00097" name="l00097"></a><span class="lineno">   97</span>    <span class="comment">/* calculate and update the mechanical angle and electric angle */</span></div>
<div class="line"><a id="l00098" name="l00098"></a><span class="lineno">   98</span>    <a class="code hl_variable" href="foc_8c.html#a6c9e1e8356380a4129a3a59d52ee1ec4">FOC_Struct</a>.<a class="code hl_variable" href="struct_f_o_c___structure__t.html#af832998676bd09b4198a7a7274072766">mechanical_angle</a> = (float) tmp_mechanical_angle * <a class="code hl_define" href="foc_8h.html#a56e6126a0ee9ac8857f6eb4cc0e720d5">MECHANGLE_COEFFICIENT</a>;</div>
<div class="line"><a id="l00099" name="l00099"></a><span class="lineno">   99</span>    <span class="keywordtype">float</span> electric_angle = (float) (tmp_mechanical_angle % (<a class="code hl_define" href="config_8h.html#a922433e3d0d117291cc2725164f21718">ENCODER_RESO</a> / <a class="code hl_define" href="config_8h.html#a9be884f546a1b98b40f8cb7971e88bfd">POLAR_PAIRS</a>)) * <a class="code hl_define" href="foc_8h.html#a5446f29b725bafeb256b404b8b8107d1">ELECANGLE_COEFFICIENT</a>;</div>
<div class="line"><a id="l00100" name="l00100"></a><span class="lineno">  100</span>    <span class="keywordflow">return</span> electric_angle;</div>
<div class="line"><a id="l00101" name="l00101"></a><span class="lineno">  101</span>}</div>
<div class="line"><a id="l00102" name="l00102"></a><span class="lineno">  102</span> </div>
<div class="line"><a id="l00106" name="l00106"></a><span class="lineno"><a class="line" href="encoder_8c.html#a82c51d78a1f7bb4a5976974ab8d7ffe5">  106</a></span><span class="keywordtype">void</span> <a class="code hl_function" href="encoder_8c.html#a82c51d78a1f7bb4a5976974ab8d7ffe5">encoder_update_speed</a>(<span class="keywordtype">void</span>) {</div>
<div class="line"><a id="l00107" name="l00107"></a><span class="lineno">  107</span>    <span class="comment">/* calculate the difference between this angle and the last angle */</span></div>
<div class="line"><a id="l00108" name="l00108"></a><span class="lineno">  108</span>    <span class="keywordtype">short</span> tmp_mechanical_angle_velocity = (short) (<a class="code hl_variable" href="encoder_8c.html#a97999bca302a3ae095acd9977fee272a">total_machine_angle</a> - <a class="code hl_variable" href="encoder_8c.html#aeb44dbae80bf7edaf6189fb8e2278f99">systick_mechanical_angle_last</a>);</div>
<div class="line"><a id="l00109" name="l00109"></a><span class="lineno">  109</span> </div>
<div class="line"><a id="l00110" name="l00110"></a><span class="lineno">  110</span>    <span class="comment">/* send it to low-pass filter for filtering to prevent PID high-frequency oscillation */</span></div>
<div class="line"><a id="l00111" name="l00111"></a><span class="lineno">  111</span>    <a class="code hl_variable" href="foc_8c.html#a6c9e1e8356380a4129a3a59d52ee1ec4">FOC_Struct</a>.<a class="code hl_variable" href="struct_f_o_c___structure__t.html#af32f899f7a231c9df64a594c7b13bfa4">rotate_speed</a> =</div>
<div class="line"><a id="l00112" name="l00112"></a><span class="lineno">  112</span>        <a class="code hl_function" href="filter_8c.html#a687b2ec6984ce9fa9d08189a3e7e1592">filter_update_value</a>((<a class="code hl_struct" href="struct_filter___structure__t.html">Filter_Structure_t</a> *) &amp;<a class="code hl_variable" href="filter_8c.html#a70331d536c7d5e99a76bd8651fd6b714">velocity_filter</a>, tmp_mechanical_angle_velocity);</div>
<div class="line"><a id="l00113" name="l00113"></a><span class="lineno">  113</span>    <a class="code hl_variable" href="encoder_8c.html#aeb44dbae80bf7edaf6189fb8e2278f99">systick_mechanical_angle_last</a> = <a class="code hl_variable" href="encoder_8c.html#a97999bca302a3ae095acd9977fee272a">total_machine_angle</a>;</div>
<div class="line"><a id="l00114" name="l00114"></a><span class="lineno">  114</span>}</div>
<div class="line"><a id="l00115" name="l00115"></a><span class="lineno">  115</span> </div>
<div class="line"><a id="l00119" name="l00119"></a><span class="lineno"><a class="line" href="encoder_8c.html#ad28e37c20d065e5014ea3a5f2de91e82">  119</a></span><span class="keywordtype">void</span> <a class="code hl_function" href="encoder_8c.html#ad28e37c20d065e5014ea3a5f2de91e82">encoder_zeroing</a>(<span class="keywordtype">void</span>) {</div>
<div class="line"><a id="l00120" name="l00120"></a><span class="lineno">  120</span>    <span class="keywordtype">float</span> u, v, w;</div>
<div class="line"><a id="l00121" name="l00121"></a><span class="lineno">  121</span>    <span class="comment">/* delay to wait for the power supply voltage to be normal */</span></div>
<div class="line"><a id="l00122" name="l00122"></a><span class="lineno">  122</span>    <a class="code hl_function" href="system_8c.html#a837b6e9a8c812f0964f71e37ac853bff">delayms</a>(1000);</div>
<div class="line"><a id="l00123" name="l00123"></a><span class="lineno">  123</span> </div>
<div class="line"><a id="l00124" name="l00124"></a><span class="lineno">  124</span>    <span class="comment">/* set that there is only a magnetic field on the straight axis. */</span></div>
<div class="line"><a id="l00125" name="l00125"></a><span class="lineno">  125</span>    <a class="code hl_function" href="foc_8c.html#a2ee2669a464e17cfaab4e084e78c4fe1">foc_calculate_dutycycle</a>(0, <a class="code hl_define" href="config_8h.html#a637f29a41ea0451a8567972018c095f0">CALI_TORQUE</a>, 0, &amp;u, &amp;v, &amp;w);</div>
<div class="line"><a id="l00126" name="l00126"></a><span class="lineno">  126</span>    <a class="code hl_function" href="timer_8c.html#ab3614c1dcfba485b2e299839d9c189e4">update_pwm_dutycycle</a>(u, v, w);</div>
<div class="line"><a id="l00127" name="l00127"></a><span class="lineno">  127</span>    <a class="code hl_function" href="system_8c.html#a837b6e9a8c812f0964f71e37ac853bff">delayms</a>(300);</div>
<div class="line"><a id="l00128" name="l00128"></a><span class="lineno">  128</span>    <a class="code hl_variable" href="encoder_8c.html#a562eacc7259f25574b1ab8ce9d7c6a3e">machine_angle_offset</a> = 0;</div>
<div class="line"><a id="l00129" name="l00129"></a><span class="lineno">  129</span>    <a class="code hl_variable" href="encoder_8c.html#a97999bca302a3ae095acd9977fee272a">total_machine_angle</a> = 0;</div>
<div class="line"><a id="l00130" name="l00130"></a><span class="lineno">  130</span> </div>
<div class="line"><a id="l00131" name="l00131"></a><span class="lineno">  131</span>    <span class="comment">/* read the angle at this time as the offset angle */</span></div>
<div class="line"><a id="l00132" name="l00132"></a><span class="lineno">  132</span>    <a class="code hl_variable" href="encoder_8c.html#a562eacc7259f25574b1ab8ce9d7c6a3e">machine_angle_offset</a> = <a class="code hl_function" href="encoder_8c.html#aa5d6a09bb36a38d53242d3b2401a0d1a">encoder_read_data</a>(0x0000) &gt;&gt; 4;</div>
<div class="line"><a id="l00133" name="l00133"></a><span class="lineno">  133</span> </div>
<div class="line"><a id="l00134" name="l00134"></a><span class="lineno">  134</span>    <span class="comment">/* zero the torque in all directions to release the motor */</span></div>
<div class="line"><a id="l00135" name="l00135"></a><span class="lineno">  135</span>    <a class="code hl_function" href="foc_8c.html#a2ee2669a464e17cfaab4e084e78c4fe1">foc_calculate_dutycycle</a>(0, 0, 0, &amp;u, &amp;v, &amp;w);</div>
<div class="line"><a id="l00136" name="l00136"></a><span class="lineno">  136</span>    <a class="code hl_function" href="timer_8c.html#ab3614c1dcfba485b2e299839d9c189e4">update_pwm_dutycycle</a>(u, v, w);</div>
<div class="line"><a id="l00137" name="l00137"></a><span class="lineno">  137</span>    <a class="code hl_function" href="system_8c.html#a837b6e9a8c812f0964f71e37ac853bff">delayms</a>(300);</div>
<div class="line"><a id="l00138" name="l00138"></a><span class="lineno">  138</span>}</div>
<div class="ttc" id="aconfig_8h_html"><div class="ttname"><a href="config_8h.html">config.h</a></div><div class="ttdoc">used to place important parameter configurations for users</div></div>
<div class="ttc" id="aconfig_8h_html_a637f29a41ea0451a8567972018c095f0"><div class="ttname"><a href="config_8h.html#a637f29a41ea0451a8567972018c095f0">CALI_TORQUE</a></div><div class="ttdeci">#define CALI_TORQUE</div><div class="ttdoc">set calibrate torque to 0.5</div><div class="ttdef"><b>Definition:</b> <a href="config_8h_source.html#l00019">config.h:19</a></div></div>
<div class="ttc" id="aconfig_8h_html_a922433e3d0d117291cc2725164f21718"><div class="ttname"><a href="config_8h.html#a922433e3d0d117291cc2725164f21718">ENCODER_RESO</a></div><div class="ttdeci">#define ENCODER_RESO</div><div class="ttdoc">SC60228 resolution is 2^8 = 4096.</div><div class="ttdef"><b>Definition:</b> <a href="config_8h_source.html#l00017">config.h:17</a></div></div>
<div class="ttc" id="aconfig_8h_html_a9be884f546a1b98b40f8cb7971e88bfd"><div class="ttname"><a href="config_8h.html#a9be884f546a1b98b40f8cb7971e88bfd">POLAR_PAIRS</a></div><div class="ttdeci">#define POLAR_PAIRS</div><div class="ttdoc">set BLDC polar pairs to 7</div><div class="ttdef"><b>Definition:</b> <a href="config_8h_source.html#l00015">config.h:15</a></div></div>
<div class="ttc" id="aencoder_8c_html_a28beb7b571ff9e6d39118c8d1324e154"><div class="ttname"><a href="encoder_8c.html#a28beb7b571ff9e6d39118c8d1324e154">encoder_get_mechanical_angle</a></div><div class="ttdeci">unsigned short encoder_get_mechanical_angle(void)</div><div class="ttdoc">read mechanical angle directly from encoder</div><div class="ttdef"><b>Definition:</b> <a href="encoder_8c_source.html#l00076">encoder.c:76</a></div></div>
<div class="ttc" id="aencoder_8c_html_a496fbd2820274bbbc7352c91415e2d3e"><div class="ttname"><a href="encoder_8c.html#a496fbd2820274bbbc7352c91415e2d3e">encoder_delay</a></div><div class="ttdeci">void encoder_delay(void)</div><div class="ttdoc">delay function for magnetic encoder</div><div class="ttdef"><b>Definition:</b> <a href="encoder_8c_source.html#l00044">encoder.c:44</a></div></div>
<div class="ttc" id="aencoder_8c_html_a562eacc7259f25574b1ab8ce9d7c6a3e"><div class="ttname"><a href="encoder_8c.html#a562eacc7259f25574b1ab8ce9d7c6a3e">machine_angle_offset</a></div><div class="ttdeci">volatile unsigned short machine_angle_offset</div><div class="ttdoc">mechanical angle offset, which is used to align the mechanical angle with the zero point of the elect...</div><div class="ttdef"><b>Definition:</b> <a href="encoder_8c_source.html#l00023">encoder.c:23</a></div></div>
<div class="ttc" id="aencoder_8c_html_a82c51d78a1f7bb4a5976974ab8d7ffe5"><div class="ttname"><a href="encoder_8c.html#a82c51d78a1f7bb4a5976974ab8d7ffe5">encoder_update_speed</a></div><div class="ttdeci">void encoder_update_speed(void)</div><div class="ttdoc">called every 2 milliseconds to calculate the speed.</div><div class="ttdef"><b>Definition:</b> <a href="encoder_8c_source.html#l00106">encoder.c:106</a></div></div>
<div class="ttc" id="aencoder_8c_html_a8fbcb19c2f15c20c1a862e0cf64f6771"><div class="ttname"><a href="encoder_8c.html#a8fbcb19c2f15c20c1a862e0cf64f6771">encoder_get_electronic_angle</a></div><div class="ttdeci">float encoder_get_electronic_angle(void)</div><div class="ttdoc">according to the electrical angle calculated from the mechanical angle, this function will call encod...</div><div class="ttdef"><b>Definition:</b> <a href="encoder_8c_source.html#l00093">encoder.c:93</a></div></div>
<div class="ttc" id="aencoder_8c_html_a97999bca302a3ae095acd9977fee272a"><div class="ttname"><a href="encoder_8c.html#a97999bca302a3ae095acd9977fee272a">total_machine_angle</a></div><div class="ttdeci">static volatile long long total_machine_angle</div><div class="ttdoc">the total mechanical angle since power on is used to calculate the angle integral value and realize t...</div><div class="ttdef"><b>Definition:</b> <a href="encoder_8c_source.html#l00034">encoder.c:34</a></div></div>
<div class="ttc" id="aencoder_8c_html_aa5d6a09bb36a38d53242d3b2401a0d1a"><div class="ttname"><a href="encoder_8c.html#aa5d6a09bb36a38d53242d3b2401a0d1a">encoder_read_data</a></div><div class="ttdeci">unsigned short encoder_read_data(unsigned short TxData)</div><div class="ttdoc">read data from the register of the magnetic encoder</div><div class="ttdef"><b>Definition:</b> <a href="encoder_8c_source.html#l00056">encoder.c:56</a></div></div>
<div class="ttc" id="aencoder_8c_html_ad28e37c20d065e5014ea3a5f2de91e82"><div class="ttname"><a href="encoder_8c.html#ad28e37c20d065e5014ea3a5f2de91e82">encoder_zeroing</a></div><div class="ttdeci">void encoder_zeroing(void)</div><div class="ttdoc">correct the mechanical angle zero deviation between the magnetic encoder and FOC.</div><div class="ttdef"><b>Definition:</b> <a href="encoder_8c_source.html#l00119">encoder.c:119</a></div></div>
<div class="ttc" id="aencoder_8c_html_aeb44dbae80bf7edaf6189fb8e2278f99"><div class="ttname"><a href="encoder_8c.html#aeb44dbae80bf7edaf6189fb8e2278f99">systick_mechanical_angle_last</a></div><div class="ttdeci">static volatile long long systick_mechanical_angle_last</div><div class="ttdoc">the mechanical angle at the last moment is used to calculate the rotation speed of the motor rotor.</div><div class="ttdef"><b>Definition:</b> <a href="encoder_8c_source.html#l00039">encoder.c:39</a></div></div>
<div class="ttc" id="aencoder_8c_html_af58d29dfca2d46874e2da41b45b48482"><div class="ttname"><a href="encoder_8c.html#af58d29dfca2d46874e2da41b45b48482">last_mechanical_angle</a></div><div class="ttdeci">static volatile unsigned short last_mechanical_angle</div><div class="ttdoc">the mechanical angle at the last moment is used to calculate the angle differential value and realize...</div><div class="ttdef"><b>Definition:</b> <a href="encoder_8c_source.html#l00029">encoder.c:29</a></div></div>
<div class="ttc" id="aencoder_8h_html"><div class="ttname"><a href="encoder_8h.html">encoder.h</a></div><div class="ttdoc">this is the header file of encoder.c.</div></div>
<div class="ttc" id="afilter_8c_html_a687b2ec6984ce9fa9d08189a3e7e1592"><div class="ttname"><a href="filter_8c.html#a687b2ec6984ce9fa9d08189a3e7e1592">filter_update_value</a></div><div class="ttdeci">float filter_update_value(Filter_Structure_t *param, short value)</div><div class="ttdoc">update the output value of the first-order low-pass filter</div><div class="ttdef"><b>Definition:</b> <a href="filter_8c_source.html#l00056">filter.c:56</a></div></div>
<div class="ttc" id="afilter_8c_html_a70331d536c7d5e99a76bd8651fd6b714"><div class="ttname"><a href="filter_8c.html#a70331d536c7d5e99a76bd8651fd6b714">velocity_filter</a></div><div class="ttdeci">volatile Filter_Structure_t velocity_filter</div><div class="ttdoc">motor rotation speed low pass filter handle</div><div class="ttdef"><b>Definition:</b> <a href="filter_8c_source.html#l00019">filter.c:19</a></div></div>
<div class="ttc" id="afilter_8h_html"><div class="ttname"><a href="filter_8h.html">filter.h</a></div><div class="ttdoc">this is the header file of filter.c, which defines the structure of filter algorithm.</div></div>
<div class="ttc" id="afoc_8c_html_a2ee2669a464e17cfaab4e084e78c4fe1"><div class="ttname"><a href="foc_8c.html#a2ee2669a464e17cfaab4e084e78c4fe1">foc_calculate_dutycycle</a></div><div class="ttdeci">void foc_calculate_dutycycle(float elect_angle, float d, float q, float *u, float *v, float *w)</div><div class="ttdoc">calculate the corresponding three-phase PWM duty cycle under the current electrical angle</div><div class="ttdef"><b>Definition:</b> <a href="foc_8c_source.html#l00099">foc.c:99</a></div></div>
<div class="ttc" id="afoc_8c_html_a6c9e1e8356380a4129a3a59d52ee1ec4"><div class="ttname"><a href="foc_8c.html#a6c9e1e8356380a4129a3a59d52ee1ec4">FOC_Struct</a></div><div class="ttdeci">volatile FOC_Structure_t FOC_Struct</div><div class="ttdoc">FOC handler.</div><div class="ttdef"><b>Definition:</b> <a href="foc_8c_source.html#l00020">foc.c:20</a></div></div>
<div class="ttc" id="afoc_8h_html"><div class="ttname"><a href="foc_8h.html">foc.h</a></div><div class="ttdoc">this is the header file of foc.c, which defines the structure of FOC algorithm and angle conversion f...</div></div>
<div class="ttc" id="afoc_8h_html_a5446f29b725bafeb256b404b8b8107d1"><div class="ttname"><a href="foc_8h.html#a5446f29b725bafeb256b404b8b8107d1">ELECANGLE_COEFFICIENT</a></div><div class="ttdeci">#define ELECANGLE_COEFFICIENT</div><div class="ttdoc">electric angle conversion factor</div><div class="ttdef"><b>Definition:</b> <a href="foc_8h_source.html#l00026">foc.h:26</a></div></div>
<div class="ttc" id="afoc_8h_html_a56e6126a0ee9ac8857f6eb4cc0e720d5"><div class="ttname"><a href="foc_8h.html#a56e6126a0ee9ac8857f6eb4cc0e720d5">MECHANGLE_COEFFICIENT</a></div><div class="ttdeci">#define MECHANGLE_COEFFICIENT</div><div class="ttdoc">mechanical angle conversion factor</div><div class="ttdef"><b>Definition:</b> <a href="foc_8h_source.html#l00024">foc.h:24</a></div></div>
<div class="ttc" id="aspi_8c_html_abbbc27ff0ee7fe2241ea2935b92d66cc"><div class="ttname"><a href="spi_8c.html#abbbc27ff0ee7fe2241ea2935b92d66cc">spi_readwrite_halfworld</a></div><div class="ttdeci">unsigned short spi_readwrite_halfworld(unsigned short data)</div><div class="ttdoc">spi0 transmit data for sc60228</div><div class="ttdef"><b>Definition:</b> <a href="spi_8c_source.html#l00019">spi.c:19</a></div></div>
<div class="ttc" id="aspi_8h_html"><div class="ttname"><a href="spi_8h.html">spi.h</a></div><div class="ttdoc">this is the header file of spi.c.</div></div>
<div class="ttc" id="astruct_f_o_c___structure__t_html_af32f899f7a231c9df64a594c7b13bfa4"><div class="ttname"><a href="struct_f_o_c___structure__t.html#af32f899f7a231c9df64a594c7b13bfa4">FOC_Structure_t::rotate_speed</a></div><div class="ttdeci">float rotate_speed</div><div class="ttdoc">motor rotate speed calculate from timer</div><div class="ttdef"><b>Definition:</b> <a href="foc_8h_source.html#l00019">foc.h:19</a></div></div>
<div class="ttc" id="astruct_f_o_c___structure__t_html_af832998676bd09b4198a7a7274072766"><div class="ttname"><a href="struct_f_o_c___structure__t.html#af832998676bd09b4198a7a7274072766">FOC_Structure_t::mechanical_angle</a></div><div class="ttdeci">float mechanical_angle</div><div class="ttdoc">mechanical angle read form encoder</div><div class="ttdef"><b>Definition:</b> <a href="foc_8h_source.html#l00018">foc.h:18</a></div></div>
<div class="ttc" id="astruct_filter___structure__t_html"><div class="ttname"><a href="struct_filter___structure__t.html">Filter_Structure_t</a></div><div class="ttdoc">structure of low-pass filter algorithm</div><div class="ttdef"><b>Definition:</b> <a href="filter_8h_source.html#l00016">filter.h:16</a></div></div>
<div class="ttc" id="asystem_8c_html_a837b6e9a8c812f0964f71e37ac853bff"><div class="ttname"><a href="system_8c.html#a837b6e9a8c812f0964f71e37ac853bff">delayms</a></div><div class="ttdeci">void delayms(unsigned long count)</div><div class="ttdoc">millisecond delay function, any time time.</div><div class="ttdef"><b>Definition:</b> <a href="system_8c_source.html#l00030">system.c:30</a></div></div>
<div class="ttc" id="asystem_8h_html"><div class="ttname"><a href="system_8h.html">system.h</a></div><div class="ttdoc">system basic function header file</div></div>
<div class="ttc" id="atimer_8c_html_ab3614c1dcfba485b2e299839d9c189e4"><div class="ttname"><a href="timer_8c.html#ab3614c1dcfba485b2e299839d9c189e4">update_pwm_dutycycle</a></div><div class="ttdeci">void update_pwm_dutycycle(float ch0, float ch1, float ch2)</div><div class="ttdoc">update timer1 ch0 1 2 duty-cycle</div><div class="ttdef"><b>Definition:</b> <a href="timer_8c_source.html#l00020">timer.c:20</a></div></div>
<div class="ttc" id="atimer_8h_html"><div class="ttname"><a href="timer_8h.html">timer.h</a></div><div class="ttdoc">this is the header file of timer.c.</div></div>
</div><!-- fragment --></div><!-- contents -->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated by&#160;<a href="https://www.doxygen.org/index.html"><img class="footer" src="doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.9.4
</small></address>
</body>
</html>
